package fr.labri.macchiato.core.model;

import fr.labri.macchiato.core.technical.FrameworkException;

/**
 * An actual model of a product. It is identified by its EAN-13 bar-code.
 * 
 * @author Matthieu FOUCAULT
 */
public class Article extends AbstractProduct implements Product {

	private String ean;
	private static final int EAN13_SIZE = 13;

	private static final String ERROR_EAN_SIZE = "The EAN bar-code must have "
			+ EAN13_SIZE + " numbers";

	Article() {
		super();
	}

	public Article(String ean, String name) {
		this();
		setName(name);
		setEan(ean);
	}

	/**
	 * 
	 * @return the model's EAN
	 */
	public String getEan() {
		return ean;
	}

	/**
	 * Sets the Model EAN
	 * 
	 * @param ean
	 *            The EAN must have 13 characters
	 */
	public void setEan(String ean) {
		if (ean.length() != EAN13_SIZE)
			throw new FrameworkException(ERROR_EAN_SIZE);
		this.ean = ean;

		notifyObservers();
	}

	/**
	 * A model equals another if and only if their EAN-13 are identical
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Article)
			return ((Article) obj).getEan().equals(this.getEan());
		else
			return false;
	}

}
